package com.example.project.service.impl;

import com.example.project.dao.IDepartmentDao;
import com.example.project.entity.Department;
import com.example.project.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *作者：金照富
 * 2023年11月3日
 */
@Service
public class IDepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private IDepartmentDao departmentMapper;
    @Override
    public int insertDepartment(Department department) {
        int departmentValue;
        Department department1=new Department();
        department1.setDepartmentName(department.getDepartmentName());
        List<Department> allDepartment=departmentMapper.getAllDepartment(department1);
        if(allDepartment.size()>=1){
            departmentValue = -1;
        }else {
            departmentValue= departmentMapper.insertDepartment(department);
        }
        return departmentValue;
    }

    @Override
    public int deleteDepartment(Department department) {
        int departmentValue;
        Department department1=new Department();
        department1.setDepartmentName(department.getDepartmentName());
        List<Department> allDepartment=departmentMapper.getAllDepartment(department1);
        if(allDepartment.size()==0){
            departmentValue = -1;
        }else {
            departmentValue= departmentMapper.deleteDepartment(department);
        }
        return departmentValue;
    }

    @Override
    public int updateDepartment(Department department) {
        int departmentValue;
        Department department1=new Department();
        department1.setDepartmentName(department.getDepartmentName());
        List<Department> allDepartment=departmentMapper.getAllDepartment(department1);
        if(allDepartment.size()>=1){
            departmentValue = -1;
        }else {
            departmentValue= departmentMapper.updateDepartment(department);
        }
        return departmentValue;
    }

    @Override
    public List<Department> getAllDepartment(Department department) {
        return departmentMapper.getAllDepartment(department);
    }

    @Override
    public Department getDepartment(Department department) {
        return departmentMapper.getDepartment(department);
    }
}
